package com.hyc.myvideo.data.adapter.my.course;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.acmenxd.toaster.Toaster;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.base.BaseListAdapter;
import com.hyc.myvideo.data.api.model.ModelCourseDetails;
import com.hyc.myvideo.utils.imguitl.ImgUitl;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 课程 详情列表 适配器
 * @author gyb
 * @version 1.0
 * @created 2017/07/21
 */
public class CourseDetailsAdapter extends BaseListAdapter<ModelCourseDetails> implements View.OnClickListener{

    private Callback callback = null;
    public CourseDetailsAdapter(Context context, List<ModelCourseDetails> objects,Callback onCallback) {
        super(context, objects);
        this.callback = onCallback;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            if (getItemViewType(position) == 0) {
                holder = new ViewHolder();
                //无数据展示
                convertView = inflater.inflate(R.layout.adapter_itme_no, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.r_no_id);
            } else {
                convertView = inflater.inflate(R.layout.activity_adapter_course_details_item, parent, false);
                holder = new ViewHolder(convertView);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(), getScreenHeight() * 2 / 3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            ModelCourseDetails courseDetails = mDataList.get(position);
            if(courseDetails != null){
                //课程名称
                holder.textViewName.setText(courseDetails.getDirect_hour_name());
                //封面
                ImgUitl.setImg(courseDetails.getDirect_hour_cover(), holder.imageViewCover,ImgUitl.dip2px(mContext,60),ImgUitl.dip2px(mContext,60));
                //设计直播时长
                String duration = String.format(mContext.getString(R.string.huc_my_course_duration), courseDetails.getDirect_hour_time_long());
                holder.textViewDuration.setText(duration);
                //时间
                 //String time = DateUtil.StringToString("2017-07-20 19:30:00", DateStyle.YYYY_MM_DD_DDD_HH_MM);
                holder.textViewTime.setText(courseDetails.getDirect_hour_start_time());
                setLiveStatus(holder,courseDetails);
            }
        }
        return convertView;
    }

    /****
     * 设置直播状态
     * @param holder

     */
    private void setLiveStatus(ViewHolder holder, final  ModelCourseDetails courseDetails ) {
        // 0未直播  1.去直播 2.完结 3.补时审批,4.审核状态，5.审核完结 6.从新直播
        holder.imageViewStatus.setVisibility(View.GONE);
        holder.buttonLive.setVisibility(View.GONE);
        holder.layoutEnd.setVisibility(View.GONE);
        holder.layoutApproval.setVisibility(View.GONE);
        holder.layoutDuration.setVisibility(View.GONE);
        holder.layoutNo.setVisibility(View.GONE);
        String duration = String.format(mContext.getString(R.string.huc_my_course_live_duration), courseDetails.getDirect_hour_now_time_long());
        switch (courseDetails.getDirect_hour_state()) {
            case 0:
                holder.buttonLive.setVisibility(View.VISIBLE);
                holder.buttonLive.setClickable(false);
                holder.buttonLive.setBackgroundResource(R.drawable.button_shape_35_1);
                holder.buttonLive.setText(R.string.home_button_live);
                break;
            case 1:
                holder.buttonLive.setVisibility(View.VISIBLE);
                holder.buttonLive.setClickable(true);
                holder.buttonLive.setBackgroundResource(R.drawable.button_shape_35_0);
                holder.buttonLive.setText(R.string.home_button_live);
                holder.buttonLive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toaster.show("去直播");
                    }
                });
                break;
           case 2:
                holder.layoutEnd.setVisibility(View.VISIBLE);
                holder.textViewPrompt.setVisibility(View.GONE);
                //展示完成图标
                holder.imageViewStatus.setVisibility(View.VISIBLE);
                holder.buttonLb.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       Toaster.show("观看录播");
                   }
                });
                break;
            case 3:
                holder.layoutApproval.setVisibility(View.VISIBLE);
                holder.layoutDuration.setVisibility(View.VISIBLE);
                //直播时长
                holder.textViewDu.setText(duration);
                //审批
                holder.buttonApproval.setOnClickListener(this);
                holder.buttonApproval.setTag(courseDetails);
                //观看录播
                holder.buttonRecord.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toaster.show("观看录播");
                    }
                });
                break;
            case 4:
                holder.layoutEnd.setVisibility(View.VISIBLE);
                holder.layoutDuration.setVisibility(View.VISIBLE);
                //直播时长
                holder.textViewDu.setText(duration);
                //审批提示
                holder.textViewPrompt.setVisibility(View.VISIBLE);
                holder.textViewPrompt.setText(courseDetails.getDirect_allow_state_name());
                //观看录播
                holder.buttonLb.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toaster.show("观看录播");
                    }
                });
                break;
            case 5:
                holder.layoutEnd.setVisibility(View.VISIBLE);
                holder.layoutDuration.setVisibility(View.VISIBLE);
                //直播时长
                holder.textViewDu.setText(duration);
                //审批提示
                holder.textViewPrompt.setVisibility(View.VISIBLE);
                holder.textViewPrompt.setText(courseDetails.getDirect_allow_state_name());
                //展示完成图标
                holder.imageViewStatus.setVisibility(View.VISIBLE);
                //观看录播
                holder.buttonLb.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toaster.show("观看录播");
                    }
                });
                break;
            case 6:
                holder.layoutNo.setVisibility(View.VISIBLE);
                //展示时长
                holder.layoutDuration.setVisibility(View.VISIBLE);
                //直播时长
                holder.textViewDu.setText(duration);
                //审批提示
                holder.textViewNoPrompt.setText(courseDetails.getDirect_allow_state_name());
                holder.buttonLb1.setVisibility(View.VISIBLE);
                //观看录播
                holder.buttonLb1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toaster.show("观看录播");
                    }
                });
                holder.buttonLb2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toaster.show("重新直播");
                    }
                });

                break;
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.b_live_approval:
                callback.onClickApproval(v);
                break;
        }

    }


    public static class ViewHolder {
        public ViewHolder() {
        }

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
        LinearLayout noDataRootLayout;
        //课程是否完结
        @BindView(R.id.course_img_status)
        ImageView imageViewStatus;

        //课程封面
        @BindView(R.id.course_img_cover)
        ImageView imageViewCover;
        //课程名称
        @BindView(R.id.current_t_name)
        TextView textViewName;
        //直播总时长
        @BindView(R.id.current_t_duration)
        TextView textViewDuration;
        //开播时间
        @BindView(R.id.current_t_time)
        TextView textViewTime;

        //未直播| 直播
        @BindView(R.id.current_live)
        Button buttonLive;

        //显示 完结
        @BindView(R.id.layout_end)
        RelativeLayout layoutEnd;
        //观看录播
        @BindView(R.id.b_live_lb)
        Button buttonLb;
        //审批提示
        @BindView(R.id.t_approval_prompt)
        TextView textViewPrompt;



        //展示审批
        @BindView(R.id.layout_approval)
        RelativeLayout layoutApproval;
        //展示录播
        @BindView(R.id.b_live_record)
        Button buttonRecord;
        //审批
        @BindView(R.id.b_live_approval)
        Button buttonApproval;



        /*展示时长*/
        @BindView(R.id.layout_duration)
        LinearLayout layoutDuration;
        @BindView(R.id.layout_t_duration)
        TextView textViewDu;
        //录播
        @BindView(R.id.b_layout_duration)
        Button buttonLb1;

        //审批未通过
        @BindView(R.id.layout_approval_no)
        RelativeLayout layoutNo;
        //重新直播
        @BindView(R.id.b_layout_approval_no)
        Button buttonLb2;
        @BindView(R.id.t_layout_approval_no)
        TextView textViewNoPrompt;
    }

    //回调借口
    public interface Callback{
        //审批
        void onClickApproval(View view);
    }




}
